*** Table 7 ***	

use "$data/simulated_df.dta", clear 


est clear
set more off
set seed 12345

foreach var in post_any_vc any_post_total_patent ///
	any_post_total_fpds_over50k any_post_total_hi_orig ///
	any_post_total_allsbir_diff {
	
	if "`var'"=="any_post_total_patent" {
		local stub "pat"
		local head "Patent"
		local yr "1095"
	}
	if "`var'"=="any_post_total_fpds_over50k" {
		local stub "fpds"
		local head "Non-SBIR Contract"
		local yr "1095"
	}
	if "`var'"=="post_any_vc" {
		local stub "vc"
		local head "VC"
		local yr "1095"
	}
	if "`var'"=="any_post_total_hi_orig" {
		local stub "orig"
		local head "High Originality"
		local yr "1095"
	}
	if "`var'"=="any_post_total_allsbir_diff" {
		local stub "allsbir"
		local yr ""
	}

	eststo `stub'_con1: areg `var'`yr' i.select##i.open $rddmain_controls ///
		pre_any_vc any_pre_total_patent any_pre_total_fpds_over50k i.topic0 ///
		in_vchub in_afbase_city any_pre_total_hi_orig [aweight=weight] ///
		if $open_first & pre_total_allsbir==0 | ///
			$conv17_first & pre_total_allsbir==0, ///
		cluster(topic_n) absorb(topic_n)
	estadd scalar R= e(r2): `stub'_con1
	estadd local pd "2017-19": `stub'_con1
	estadd local prop "First": `stub'_con1
	estadd local controls "Yes": `stub'_con1
	estadd local techfe "No": `stub'_con1
	qui sum `var'`yr' if $open_first & pre_total_allsbir==0 | ///
			$conv17_first & pre_total_allsbir==0
	estadd scalar ymean = r(mean): `stub'_con1
	
	eststo `stub'_con2: areg `var'`yr' i.select##i.open $rddmain_controls ///
		pre_any_vc any_pre_total_patent any_pre_total_fpds_over50k ///
		employees_at_app age i.full_tag_n25 i.topic0 any_pre_total_hi_orig ///
		in_vchub in_afbase_city [aweight=weight] ///
		if $open_first & pre_total_allsbir==0 | ///
			$conv17_first & pre_total_allsbir==0, ///
		cluster(topic_n) absorb(topic_n)
	estadd scalar R= e(r2): `stub'_con2
	estadd local pd "2017-19": `stub'_con2
	estadd local prop "First": `stub'_con2
	estadd local controls "Yes": `stub'_con2
	estadd local techfe "Yes": `stub'_con2
	qui sum `var'`yr' if $open_first & pre_total_allsbir==0 | ///
			$conv17_first & pre_total_allsbir==0
	estadd scalar ymean = r(mean): `stub'_con2
	
	eststo `stub'_bw1: areg `var'`yr' i.select##i.open if $open_first & ///
			pre_total_allsbir==0 & bw2a==1 | $conv17_first & ///
			pre_total_allsbir==0 & bw2a==1, ///
		cluster(topic_n) absorb(topic_n)
	estadd scalar R= e(r2): `stub'_bw1
	estadd local pd "2017-19": `stub'_bw1
	estadd local prop "First": `stub'_bw1
	estadd local controls "No": `stub'_bw1
	estadd local techfe "No": `stub'_bw1
	qui sum `var'`yr' if $open_first & pre_total_allsbir==0 & bw2a==1 | ///
			$conv17_first & pre_total_allsbir==0 & bw2a==1
	estadd scalar ymean = r(mean): `stub'_bw1
	ritest select (_b[1.select#1.open]), ///
		reps(10) strata(topic_n): areg `var'`yr' i.select##i.open ///
		if $open_first & pre_total_allsbir==0 & bw2a==1  | $conv17_first & ///
			pre_total_allsbir==0 & bw2a==1 , ///
		a(topic_n)
	matrix pvalues=r(p) //save the p-values from ritest
	estadd scalar rip = pvalues[1,1]: `stub'_bw1
	
	eststo `stub'_bw2: areg `var'`yr' i.select##i.open ///
		employees_at_app age i.full_tag_n25 if $open_first & pre_total_allsbir==0 ///
			& bw2a==1 | $conv17_first & pre_total_allsbir==0 & bw2a==1, ///
		cluster(topic_n) absorb(topic_n)
	estadd scalar R= e(r2): `stub'_bw2
	estadd local pd "2017-19": `stub'_bw2
	estadd local prop "First": `stub'_bw2
	estadd local controls "Yes": `stub'_bw2
	estadd local techfe "Yes": `stub'_bw2
	qui sum `var'`yr' if $open_first & pre_total_allsbir==0 & bw2a==1 | ///
			$conv17_first & pre_total_allsbir==0 & bw2a==1
	estadd scalar ymean = r(mean): `stub'_bw2
	ritest select (_b[1.select#1.open]), ///
		reps(10) strata(topic_n): areg `var'`yr' i.select##i.open ///
		employees_at_app age i.full_tag_n25 ///
		if $open_first & pre_total_allsbir==0 & bw2a==1  | $conv17_first & ///
			pre_total_allsbir==0 & bw2a==1 , ///
		a(topic_n)
	matrix pvalues=r(p) //save the p-values from ritest
	estadd scalar rip = pvalues[1,1]: `stub'_bw2
	
	eststo `stub'_all1: areg `var'`yr' i.select $rddmain_controls ///
		[aweight=weight] if $conv_all & year < 2017 , ///
		cluster(topic_n) absorb(topic_n)
	estadd scalar R= e(r2): `stub'_all1
	estadd local pd "2003-16": `stub'_all1
	estadd local prop "First": `stub'_all1
	estadd local controls "No": `stub'_all1
	estadd local techfe "No": `stub'_all1
	qui sum `var'`yr' if firm_first_everconv_proposal ==1 & phase==1 & conv==1  & year < 2017 
	estadd scalar ymean = r(mean): `stub'_all1
	
	eststo `stub'_all2: areg `var'`yr' i.select $rddmain_controls ///
		employees_at_app age i.full_tag_n25 [aweight=weight] if ///
			$conv_all & year < 2017   , ///
		cluster(topic_n) absorb(topic_n)
	estadd scalar R= e(r2): `stub'_all2
	estadd local pd "2003-16": `stub'_all2
	estadd local prop "First": `stub'_all2
	estadd local controls "Yes": `stub'_all2
	estadd local techfe "Yes": `stub'_all2
	qui sum `var'`yr' if  firm_first_everconv_proposal ==1 & phase==1 & conv==1  & year < 2017 
	estadd scalar ymean = r(mean): `stub'_all2
	

	eststo `stub'_eo1: areg `var'`yr' i.select $rddmain_controls ///
		[aweight=weight] if $conv_all & year < 2017 & ever_open==0, ///
		cluster(topic_n) absorb(topic_n)
	estadd scalar R= e(r2): `stub'_eo1
	estadd local pd "2003-16": `stub'_eo1
	estadd local prop "First": `stub'_eo1
	estadd local controls "No": `stub'_eo1
	estadd local techfe "No": `stub'_eo1
	qui sum `var'`yr' if firm_first_everconv_proposal ==1 & phase==1 & conv==1  & year < 2017 & ever_open==0
	estadd scalar ymean = r(mean): `stub'_eo1
	
	eststo `stub'_eo2: areg `var'`yr' i.select $rddmain_controls ///
		employees_at_app age i.full_tag_n25 [aweight=weight] if ///
			$conv_all & year < 2017  & ever_open==0, ///
		cluster(topic_n) absorb(topic_n)
	estadd scalar R= e(r2): `stub'_eo2
	estadd local pd "2003-16": `stub'_eo2
	estadd local prop "First": `stub'_eo2
	estadd local controls "Yes": `stub'_eo2
	estadd local techfe "Yes": `stub'_eo2
	qui sum `var'`yr' if  firm_first_everconv_proposal ==1 & phase==1 & conv==1  & year < 2017 & ever_open==0
	estadd scalar ymean = r(mean): `stub'_eo2
	
}

esttab fpds_con* vc_con* pat_con* orig_con* allsbir_con* using "$tables/table7a.tex", ///
	$general $labels ///
	keep(1.select 1.select#1.open) ///
	scalars("prop Proposal" "controls Additional Controls" "techfe Lifecycle Controls \& Narrow Tech FE" "ymean Outcome Mean") ///
	postfoot("\midrule \end{tabular} \end{footnotesize} ") ///
	prehead("\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{footnotesize} \begin{tabular}{l*{10}{>{\centering\arraybackslash}p{0.09\textwidth}}}  \toprule \multicolumn{11}{c}{Panel A: Additional Controls (2017-19)} \\ \midrule & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(DoD Contract)}} & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(VC)}}  & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(Patent)}} & \multicolumn{2}{c}{$\mathbbm{1}$(High-Originality} & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$($> 2$ SBIR)}} \\ & & & & && & \multicolumn{2}{c}{Patent)} \\ \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(lr){8-9} \cmidrule(lr){10-11} ")
	
esttab fpds_bw* vc_bw* pat_bw* orig_bw* allsbir_bw* using "$tables/table7b.tex", ///
	$general $labels ///
	keep(1.select 1.select#1.open) ///
	scalars("prop Proposal" "rip Randomization Inference \$p\$-value" "ymean Outcome Mean") ///
	postfoot("\midrule \end{tabular} \end{footnotesize}") ///
	prehead("\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{footnotesize} \begin{tabular}{l*{10}{>{\centering\arraybackslash}p{0.09\textwidth}}} \\ \multicolumn{11}{c}{Panel B: Narrow Bandwidth} \\  & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(DoD Contract)}} & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(VC)}}  & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(Patent)}} & \multicolumn{2}{c}{$\mathbbm{1}$(High-Originality} & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$($> 2$ SBIR)}} \\ & & & & && & \multicolumn{2}{c}{Patent)} \\ \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(lr){8-9} \cmidrule(lr){10-11} ")

esttab fpds_all* vc_all* pat_all* orig_all* allsbir_all* using "$tables/table7c.tex", ///
	$general $labels ///
	keep(1.select) ///
	scalars("ymean Outcome Mean") ///
	postfoot("\midrule \end{tabular} \end{footnotesize} ") ///
	prehead("\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{footnotesize} \begin{tabular}{l*{10}{>{\centering\arraybackslash}p{0.09\textwidth}}} \multicolumn{11}{c}{Panel C: Conventional Program Before Open} \\ \midrule & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(DoD Contract)}} & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(VC)}}  & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(Patent)}} & \multicolumn{2}{c}{$\mathbbm{1}$(High-Originality} & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$($> 2$ SBIR)}} \\ & & & & && & \multicolumn{2}{c}{Patent)} \\ \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(lr){8-9} \cmidrule(lr){10-11} ")

esttab fpds_eo* vc_eo* pat_eo* orig_eo* allsbir_eo* using "$tables/table7d.tex", ///
	$general $labels ///
	keep(1.select) ///
	scalars("prop Proposal" ///
		    "pd Time Period" "ymean Outcome Mean" "controls Controls" "techfe Narrow Tech FE") ///
	postfoot("\bottomrule \end{tabular} \end{footnotesize} ") ///
	prehead("\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{footnotesize} \begin{tabular}{l*{10}{>{\centering\arraybackslash}p{0.09\textwidth}}} \multicolumn{11}{c}{Panel D: Conventional Program Before Open, Among Firms that Never Applied to Open} \\ \midrule& \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(DoD Contract)}} & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(VC)}}  & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(Patent)}} & \multicolumn{2}{c}{$\mathbbm{1}$(High-Originality} & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$($\geq 2$ SBIR)}} \\ & & & & && & \multicolumn{2}{c}{Patent)} \\ \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(lr){8-9} \cmidrule(lr){10-11} ")
